var Artist = Entity.extend('artist', 
	{
		name: String,
		portrait_path: String
	},
	{
	toString: function() {
		return $pick(this.name, '[Artist Object]');
	}
});
Artist.setConstraint('name', CONSTRAINT_NOT_NULL);

var Album = Entity.extend('album', 
	{
		title: String,
		cover_path: String,
		artist: Artist
	}, 
	{
	toString: function() {
		return $pick(this.title, '[Album Object]');
	}
});
Album.setConstraint('title', CONSTRAINT_NOT_NULL);

var Track = Entity.extend('track', 
	{
		title: String,
		track: SqlInteger,
		length: Time,
		path: String,
		year: SqlInteger,
		comment: String,
		album: Album,
		artist: Artist
	},
	{
	toString: function() {
		return this.title ? this.artist.name + " - " + this.title : $pick(this.path,'[Track object]');
	}
});
Track.setConstraint('artist', CONSTRAINT_NOT_NULL);
Track.setConstraint('title', CONSTRAINT_NOT_NULL);
Track.setConstraint('path', CONSTRAINT_NOT_NULL);

Artist.hasMany('albums', Album, 'artist');
Artist.hasMany('tracks', Track, 'artist');
Album.hasMany('tracks', Track, 'album');

Track.get = function (filename) {
	if (!Track.$tagReader) {
		Track.$tagReader = COM.createObject("TagReader");
	}
	var info = Track.$tagReader.ReadTrackInfoFile(filename);
	var artist = Artist.createOrFind({
		name: info.ArtistName
	});
	var album = Album.createOrFind({
		title: info.AlbumTitle
	});
	var track = Track.createOrFind({
		title: info.TrackTitle,
		artist: artist,
		album: album,
		path: filename
	});
	track.track = new SqlInteger(info.TrackNumber);
	track.length = new Time({second: info.TrackLength.toInt()});
	track.saveOrUpdate();
	return track;
}